package com.gosophiaigit.common.busi.service.impl;


import java.util.List;


import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springside.core.dao.HibernateEntityDao;

import com.gosophiaigit.common.busi.service.IGroupUtilService;
import com.gosophiaigit.contact.busi.entity.Contact;
import com.gosophiaigit.contact.busi.entity.PositionUser;

/**
 * 组和联系人操作帮助实现，包括权限和得到组织机构相关信息
 * 
 * @author guodp
 * 
 */
@Service("groupUtilService")
@Transactional
@SuppressWarnings("unchecked")
public class GroupUtilServiceImpl implements IGroupUtilService {

	@Autowired
	private HibernateEntityDao<?> hibernateEntityDao;

	

	/**
	 * 根据机构ID查询机构下的人员
	 */
	@Override
	public List<Contact> findContactByOrgId(Long orgId, Long userAccountId) {
		Assert.notNull(orgId, "组信息ID不能为null");
		boolean permission = findPermissionByUserAccountId(userAccountId);
		String query_sql;
			query_sql = "select  distinct(t.contactid) as contactId, f.username as contactName ,t.useflag as contactUseFlag from t_orgmanage_position o join t_Orgmanage_Position_User "
				+ "p on o.orgId = "
				+ orgId
				+ " and o.positionid = p.position_id join t_usermana_userinfo f "
				+ "on p.userinfoid = f.userinfoid join igit_contact t on f.userinfoid = t.userinfoid where (t.delflag is null or t.delflag <>1)";
		if (!permission) {
			query_sql += " and t.useflag=1";
		}
		Query query = hibernateEntityDao.getSessionFactory()
				.getCurrentSession().createSQLQuery(query_sql).addScalar(
						"contactId", Hibernate.LONG).addScalar("contactName")
				.addScalar("contactUseFlag", Hibernate.LONG)
				.setResultTransformer(Transformers.aliasToBean(Contact.class));
		List<Contact> resultList = query.list();
		return resultList;
	}

	/**
	 * 查询登录人是否拥有通讯录操作权限
	 */
	@Override
	public boolean findPermissionByUserAccountId(Long userAccountId) {
		List list;
		String sql = "select * from t_usermana_permission p " 
				   + "left join t_Usermana_Userpermission u on p.permissionid=u.permissionid " 
				   + "where p.permissionname='通讯录操作' and u.permissionstatus=1 and u.useraccountid ="+ userAccountId;
		list = hibernateEntityDao.getSessionFactory().getCurrentSession()
				.createSQLQuery(sql).list();
		if (list.size() == 1) {
			return true;
		}
		return false;
	}
	
	/** 通过其ID查询数据字典数据 */
	public String queryDataDictionaryById(Long Id) {
		String sql = "select value from t_datadictionary_datavalue where DATAVALUEID="
				+ Id;
		String contactType = (String) hibernateEntityDao.getSessionFactory()
				.getCurrentSession().createSQLQuery(sql).uniqueResult();
		return contactType;
	}

	/**
	 * 通过用户ID查询用户名称
	 */
	public String getUserInfoName(Long userInfoId) {
		String sql = "select USERNAME from T_USERMANA_USERINFO where USERINFOID="
				+ userInfoId;
		String userName = (String) hibernateEntityDao.getSessionFactory()
				.getCurrentSession().createSQLQuery(sql).uniqueResult();
		return userName;
	}

	/**
	 * 获得人员和职位关联实体.
	 * 
	 * @param userInfoId用户Id
	 */
	public PositionUser getPositionUserByUserinfoId(Long userInfoId) {
	    String hql="from PositionUser where userInfoId=?";
	    List<PositionUser> list= hibernateEntityDao.find(hql, userInfoId);
	    return list.size() > 0 ? list.get(0) : null;
	}
}
